﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Animation;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;

namespace Trippy.CollidableGameComponents.Objects
{
    class Spikes : CollidableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }

        protected override void LoadContent()
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Objects/spikes");
            Sprite = new AnimatedSprite(this, SpriteSheet, SpriteSheet.Height, SpriteSheet.Width, 0, 0, -5, 10, 1, 0f);
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }

         public Spikes(Level level)
            : base (level)
        {
            
        }

        public static bool AnimationActive = false;

        protected override void CollisionHandler(CollidableGameComponent component, CollisionSide side)
        {

            if (component is Agent)
            {
                Agent a = (Agent)component;
                if (a is Hero || a is Enemy)
                {
                    switch (side)
                    {
                        case CollisionSide.Top:
                            a.HitGround(BoundingBox.Top);
                            a.MarkForDeletion(Hero.MannerOfDeath.Obstacles);
                            if (AnimationActive == false)
                            {
                                new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Pow,
                                    new Vector2(a.Position.X, BoundingBox.Top), delegate { Spikes.AnimationActive = false; });
                                AnimationActive = true;
                            }
                            break;
                        case CollisionSide.Right:
                            if (component.Velocity.Y >= 100f)
                            {
                                a.MarkForDeletion(Hero.MannerOfDeath.Obstacles);
                                if (AnimationActive == false)
                                {
                                    new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Pow,
                                        new Vector2(a.Position.X, BoundingBox.Top), delegate { Spikes.AnimationActive = false; });
                                    AnimationActive = true;
                                }
                            }
                            else
                                a.HitLeftWall(BoundingBox.Right);
                            break;
                        case CollisionSide.Bottom:
                            a.HitCeiling(BoundingBox.Bottom);
                            break;
                        case CollisionSide.Left:
                            if (component.Velocity.Y >= 100f)
                            {
                                a.MarkForDeletion(Hero.MannerOfDeath.Obstacles);
                                if (AnimationActive == false)
                                {
                                    new EffectAnimation(CurrentLevel, EffectAnimation.Effects.Pow,
                                        new Vector2(a.Position.X, BoundingBox.Top), delegate { Spikes.AnimationActive = false; });
                                    AnimationActive = true;
                                }
                            }
                            else
                                a.HitRightWall(BoundingBox.Left);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("side");
                    }
                }
            }
            base.CollisionHandler(component, side);
        }
    }
}
